package com.fyt.algorithm.Leecode.day4;

import java.util.HashSet;
import java.util.Set;

public class LongestConsecutive {

    public static int longestConsecutive(int[] nums) {
        Set<Integer> set=new HashSet<>();
        //元素去重
        for (int num:nums){
            set.add(num);
        }
        int longestStreak=0;
        for(int num:set){
            if(!set.contains(num-1)){
                //那么他可能是一个最长子序列的开头
//                int current=num;
                int streak=1;
                while (set.contains(num+1)){
                    num++;
                    streak++;
                }
                longestStreak=Math.max(longestStreak,streak);

            }

        }



        return  longestStreak;
    }

    public static void main(String[] args) {
        int []arr={100,4,200,1,3,2};
        System.out.println(longestConsecutive(arr));
    }
}
